package com.cn.wanxi.publisher.config;

import com.cn.wanxi.publisher.util.ParamUtil;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitMQConfig
 * @Description 交换机、消息队列的相关配置
 * @Author JiJiang
 * @Date 2022/9/28 20:59
 * @Version 1.0
 */
@Configuration
public class RabbitMQConfig {


    //region 用户注册
    @Bean
    Queue registerQueue() {

        return new Queue(ParamUtil.QUEUE_NAME_REGISTER, true);
    }


    @Bean
    TopicExchange userExchange() {
        return new TopicExchange(ParamUtil.EXCHANGE_NAME_USER, true, false);
    }


    @Bean
    Binding registerQueue2WxUserTopic(Queue registerQueue, TopicExchange userExchange) {

        return BindingBuilder.bind(registerQueue).to(userExchange).with(ParamUtil.BINDING_KEY_NAME);
    }
    //endregion


    //region 线程发送消息
    @Bean
    Queue threadQueue() {

        return new Queue(ParamUtil.QUEUE_NAME_THREAD, true);
    }


    @Bean
    TopicExchange threadExchange() {
        return new TopicExchange(ParamUtil.EXCHANGE_NAME_THREAD, true, false);
    }


    @Bean
    Binding threadQueue2ThreadTopic(Queue threadQueue, TopicExchange threadExchange) {


        return BindingBuilder.bind(threadQueue).to(threadExchange).with(ParamUtil.BINDING_KEY_NAME_THREAD);
    }
    //endregion


    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }


}
